#include <iostream>
#include <sys/socket.h>
#include <sys/types.h>
#include <pwd.h>
#include "../../socks/sock.h"
#include "../../socks/sockdist.h"

using namespace std;

int main(int argc, char** argv)
{
	if(argc > 0)
	{
		Sock BRpublic(SOCK_STREAM, (short)54001, 0);
		int descBRpublic;
		
		if(BRpublic.good())
		{
			descBRpublic = BRpublic.getsDesc();
		}
		
		while(true)
		{
			int res = listen(descBRpublic, 5);
			struct sockaddr_in BRcv;
			socklen_t lgBRcv = sizeof(struct sockaddr_in);
			int descBRcv = accept(descBRpublic, (struct sockaddr*)&BRcv, &lgBRcv);
			
			if(descBRcv != -1)
			{
				cout << "Connection : OK" << endl;
				cout << "Num Port : " << ntohs(BRcv.sin_port) << endl;
				
				uid_t uID;
				int resRecv = recv(descBRcv, &uID, sizeof(uID), 0);
				
				if(resRecv != -1)
				{
					string buff;
					struct passwd* pw = getpwuid(uID);
					buff = pw->pw_name;
					cout << buff << endl;
					
					buff = "Bonjour, tu es "+buff+", ";
					string pass = pw->pw_passwd;
					buff += pass;
					
					int resSend = send(descBRcv, buff.c_str(), buff.length(), 0);
				}
				else
				{
					cout << "R" << endl;
				}
			}
			else
			{
				cout << "Connection : Error" << endl;
			}
		}
	}
}
 
 
 
